<template>
  <div class="tpl-message">
    <Card :dis-hover="true">
      <div id="content">
        <Alert type="info">
          需要设置模板消息所属行业为：
          <span style="color: red;">IT科技/IT软件与服务</span>
        </Alert>
        <Form ref="form_data" :model="form_data" :rules="rule_form_data">
          <FormItem label="模板消息id，通用待审核通知" prop="await_check_tpl">
            <Input v-model="form_data.await_check_tpl" placeholder="通用待审核模板消息通知"></Input>
          </FormItem>

          <FormItem label="模板消息id，通用审核结果通知" prop="result_check_tpl">
            <Input v-model="form_data.result_check_tpl" placeholder="通用待审核模板消息通知"></Input>
          </FormItem>

          <FormItem>
<!--            <Button type="primary" @click="submit('form_data')">确认设置</Button>-->
            <Button type="primary" @click="autoSet()">一键配置</Button>
          </FormItem>
        </Form>
      </div>
    </Card>
  </div>
</template>

<script>
// 待审核提醒 模板编号 OPENTM418073234
// 审核结果通知  模板编号 OPENTM417949050
export default {
  name: 'tpl-message',
  data () {
    return {
      form_data: {
        await_check_tpl: '',
        result_check_tpl: ''
      },
      rule_form_data: {
        await_check_tpl: [
          { required: true, message: '请设置模板消息id', trigger: 'blur' }
        ],
        result_check_tpl: [
          { required: true, message: '请设置模板消息id', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    data () {
      let { await_check_tpl, result_check_tpl } = this.form_data
      return {
        await_check_tpl,
        result_check_tpl
      }
    }
  },
  mounted () {
    this.find()
  },
  methods: {
    // Run
    /**
     * 获取参数
     */
    find () {
      this.$get('settingMessage/find')
        .then((res) => {
          let { code, data } = res.data
          if (code === 0) {
            let { await_check_tpl, result_check_tpl } = data
            this.form_data = {
              await_check_tpl,
              result_check_tpl
            }
          }
        })
    },
    /**
     * 提交设置参数
     */
    submit (name) {
      this.$refs[name].validate((valid) => {
        if (valid) {
          this.$post('settingMessage/set', {
            data: this.data
          })
            .then((res) => {
              if (res.data.code === 0) {
                this.$Message.success('设置成功')
              } else {
                this.$Message.error('设置失败')
              }
            })
        }
      })
    },
    /**
     * 一键配置模板id
     */
    autoSet () {
      this.$post('settingMessage/autoSetTplMessage')
        .then((res) => {
          let { code, data, err_msg } = res.data
          if (!code) {
            this.form_data = {
              await_check_tpl: data.await_check_tpl,
              result_check_tpl: data.result_check_tpl
            }
            this.$Message.success('配置成功')
          } else {
            this.$Message.error(err_msg)
          }
        })
    }
  }
}
</script>

<style scoped lang="less">
  #content{
    width: 600px;
  }
</style>
